const userModel = require('../model/userModel');
module.exports = {
  user_login(req, res) {
    let data = req.body;
    let response = {
      code: 0,
      message: 'error'
    }
    userModel.getUserInfoByEmailAndPassword(data.email, data.password, (err, result) => {
      if (err) throw err;
      if (result) {
        let userInfo = result[0];
        if (userInfo.status == "activated") {
          // 写入session
          req.session.userInfo = userInfo;
          response.code = 1;
          response.message = 'OK'
          response.oldUrl = req.session.oldUrl ? req.session.oldUrl : '/admin/index';
        }
      }
      res.json(response);
    });
  },

  user_getHeadAndNickname(req, res) {
    let response = {
      code: 0,
      message: '还没有登录'
    }
    if (req.session && req.session.userInfo) {
      let id = req.session.userInfo.id;
      userModel.getUserHeadAndNickname(id, (err, result) => {
        if (err) throw err;
        let data = result[0];
        response.code = 1;
        response.message = 'ok';
        response.data = data;

        res.json(response);
      })
    }
  },

  getAllUser(req, res) {
    let response = {
      code: 0,
      message: 'error'
    };
    userModel.getAllUser((err, result) => {
      if (err) throw err;
      response.code = 1;
      response.message = 'ok';
      response.data = result;

      res.json(response);
    })
  },

  addNewUser(req, res) {
    let response = {
      code: 0,
      message: 'error'
    };
    let data = req.body;
    // 邮箱不能重复
    userModel.getUserByEmail(data.email, (err, result) => {
      if (err) throw err;
      let count = result.length;
      if (count > 0) {
        response.message = "邮箱不能重复";
        res.json(response);
      } else {
        userModel.addNewUser(data, (err, result) => {
          if (err) throw err;
          response.code = 1;
          response.message = '添加成功';

          res.json(response);
        })
      }
    })
  },

  getUserInfoById(req, res) {
    let response = {
      code: 0,
      message: 'error'
    }
    let id = req.body.id;
    userModel.getUserInfoById(id, (err, result) => {
      if (err) throw err;
      response.code = 1;
      response.message = 'ok';
      response.data = result[0];

      res.json(response);
    })
  },

  updateUserInfo(req, res) {
    let response = {
      code: 0,
      message: 'error'
    }
    let data = req.body;
    let id = data.id;
    delete data.id;
    userModel.updateUserInfoById(id, data, (err, result) => {
      if (err) throw err;
      response.code = 1;
      response.message = '修改成功';

      res.json(response);
    })
  },

  delUserById(req, res) {
    let response = {
      code: 0,
      message: 'error'
    }
    let id = req.body.id;
    userModel.delUserById(id, (err, result) => {
      if (err) throw err;
      response.code = 1;
      response.message = 'ok';
      response.data = result[0];

      res.json(response);
    })
  },

  delMultiple(req, res) {
    let response = {
      code: 0,
      message: 'error'
    }
    let ids = req.query.ids;
    userModel.delMultiple(ids, (err, result) => {
      if (err) throw err;
      response.code = 1;
      response.message = 'ok';
      response.data = result[0];

      res.json(response);
    })
  }
}